﻿using Nx.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Nx.Clients.Tests.Controllers
{
    public class TestController : ApiController
    {
        private ILogger Logger;
        /// <summary>
        /// The Constructor. Intentionally not parameterless to verify that IoC is working properly.
        /// </summary>
        /// <param name="logFactory">A standard Nx.Logging.LogFactory</param>
        public TestController(ILogFactory logFactory)
        {
            Logger = logFactory.CreateLogger("TestController");
        }

        ~TestController()
        {
            Dispose(false);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                Logger.Dispose();
            }

            base.Dispose(disposing);
        }

        [HttpGet]
        public IEnumerable<int> Get()
        {
            Logger.Info("Processing GET request : {0}", ControllerContext.Request.RequestUri);
            return Enumerable.Range(0, 5);
        }

        [HttpGet]
        public string Get(string id)
        {
            Logger.Info("Processing GET(id) request : {0}", ControllerContext.Request.RequestUri);
            return id;
        }

        [HttpPost]
        public HttpResponseMessage Post([FromBody] string id)
        {
            HttpResponseMessage response = null;
            try
            {
                Logger.Info("Processing POST request : {0}", ControllerContext.Request.RequestUri);
                response = Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }

            return response;
        }
    }
}
